package me.zk.practice1.s;

/**
 * @author zhangkai
 * @date 2023/7/13 12:58
 */
public class LS_Offer_52 {
    ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int l1 = 0,l2=0;
        ListNode a = headA,b=headB;
        while (a!=null) {
            l1++;
            a=a.next;
        }
        while (b!=null) {
            l2++;
            b=b.next;
        }
        if (l1 >l2) {
            for (int i = 0; i < l1-l2; i++) {
                headA = headA.next;
            }
        } else {
            for (int i = 0; i < l2-l1; i++) {
                headB = headB.next;
            }
        }
        while (headA != headB) {
            headA = headA.next;
            headB = headB.next;
        }
        return headA;
    }

    ListNode getIntersectionNode2(ListNode headA, ListNode headB) {
        if (headA ==null || headB ==null) {
            return null;
        }
        ListNode pa = headA;
        ListNode pb = headB;
        while (pb != pa) {
            pa = pa==null ? headB:pa.next;
            pb = pb==null ? headA:pb.next;
        }
        return pa;
    }
}
